// Copyright 2015-2019 Mail.Ru Group. All Rights Reserved.

package com.pushkinstudio.PsFacebookMobile;

import com.epicgames.ue4.GameActivity;

import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsConstants;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.lang.String;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class PsFacebookMobile
{
    public native void nativeFacebookLoginCompleted(boolean bSuccess, String token);

    private GameActivity _activity;
    private CallbackManager _callbackManager;
    private AppEventsLogger logger;

    private static final String LOGTAG = "UE4-PS-FACEBOOK";

    public PsFacebookMobile(GameActivity activity) 
    {
        Log.d(LOGTAG, "Init");

        _activity = activity;
        _callbackManager = CallbackManager.Factory.create();
        logger = AppEventsLogger.newLogger(activity.getApplication());
    }

    public boolean onActivityResult(int requestCode, int resultCode, Intent data)
    {
        return _callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    public boolean Login(String LoginPermissions)
    {
        Log.d(LOGTAG, "Login: " + LoginPermissions);

        try
        {
            AccessToken accessToken = AccessToken.getCurrentAccessToken();
            if (accessToken == null || accessToken.isExpired())
            {
                LoginManager.getInstance().registerCallback(
                    _callbackManager,
                    new FacebookCallback<LoginResult>()
                    {
                        @Override
                        public void onSuccess(LoginResult loginResult)
                        {
                            Log.d(LOGTAG, "Login Success");
                            nativeFacebookLoginCompleted(true, AccessToken.getCurrentAccessToken().getToken());
                        }

                        @Override
                        public void onCancel()
                        {
                            Log.d(LOGTAG, "Login Cancel");
                            nativeFacebookLoginCompleted(false, "");
                        }

                        @Override
                        public void onError(FacebookException e)
                        {
                            Log.d(LOGTAG, "Login Error: " + e.toString());
                            nativeFacebookLoginCompleted(false, "");
                        }
                    }
                );

                HashSet<String> loginPermissions = new HashSet(Arrays.asList(TextUtils.split(LoginPermissions, ",")));
                LoginManager.getInstance().logIn(_activity, loginPermissions);
            }
            else
            {
                Log.d(LOGTAG, "User is already logged in");
                nativeFacebookLoginCompleted(true, accessToken.getToken());
            }

            return true;
        }
        catch (Exception e)
        {
            Log.d(LOGTAG, e.toString());
        }

        nativeFacebookLoginCompleted(false, "");
        return false;
    }

    public boolean Logout()
    {
        Log.d(LOGTAG, "Logout");

        try
        {
            LoginManager.getInstance().logOut();
            return true;
        }
        catch (Exception e)
        {
            Log.d(LOGTAG, "Logout Error: " + e.toString());
        }

        return false;
    }

    public boolean IsLoggedIn()
    {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        return accessToken != null && !accessToken.isExpired();
    }

    // ======== 埋点相关 ========
    public void logAchieveLevelEvent (String level) {
        Bundle params = new Bundle();
        params.putString(AppEventsConstants.EVENT_PARAM_LEVEL, level);
        logger.logEvent(AppEventsConstants.EVENT_NAME_ACHIEVED_LEVEL, params);
    }

    public void _logSpendXEvent ( String x, double totalValue, String qudao ) {
        Bundle params = new Bundle();
        params.putString(AppEventsConstants.EVENT_PARAM_CONTENT, qudao);
        params.putString(AppEventsConstants.EVENT_PARAM_CONTENT_ID, x);
        params.putString(AppEventsConstants.EVENT_PARAM_CONTENT_TYPE, "product");
        logger.logEvent(AppEventsConstants.EVENT_NAME_SPENT_CREDITS, totalValue, params);
    }

    public void logSpendGoldEvent ( double totalValue, String qudao ) {
        _logSpendXEvent("gold", totalValue, qudao);
    }

    public void logSpendFakeDiamondEvent ( double totalValue, String qudao ) {
        _logSpendXEvent("fakediamond", totalValue, qudao);
    }

    public void logSpendDiamondEvent ( double totalValue, String qudao ) {
        _logSpendXEvent("diamond", totalValue, qudao);
    }

    // use it like "logCustomEventWithSum('gain_pet', 'PetID', 'Dog01', 1.0)"
    public void logCustomEventWithSum ( String eventName,String param1Name, String param1Value, double param2) {
        Bundle params = new Bundle();
        params.putString(param1Name, param1Value);
        logger.logEvent(eventName, param2, params);
    }
}
